<?php

namespace App\Http\Controllers\Owner;

use App\Imports\DriverImport;
use App\Models\DriverMailList;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Owner\BaseController;

/**司机列表
 * Class DriverController
 * @package App\Http\Controllers\Owner
 */
class DriverController extends BaseController
{


    public function index(Request $request)
    {

        return view('owner.driver.index');


    }

    public function indexSelect(Request $request)
    {
        return view('owner.driver.select');


    }

    //获取当前货主的司机列表

    public function data(Request $request)
    {

        $ownerId = $this->owner_id;
        //  DB::enableQueryLog();
        $model = DriverMailList::query()
            ->where('role_id', DriverMailList::ROLE_DRIVER)
            ->where('owner_id', $ownerId)
            ->where('status', 1);


        if ($request->get('name')) {
            $model = $model->where('name', 'like', '%' . $request->get('name') . '%');
        }
        if ($request->get('phone')) {
            $model = $model->where('phone', 'like', '%' . $request->get('phone') . '%');
        }
        if ($request->get('card_no')) {
            $model = $model->where('card_no', 'like', '%' . $request->get('card_no') . '%');
        }
        if ($request->get('car_no')) {
            $model = $model->where('car_no', 'like', '%' . $request->get('car_no') . '%');
        }
        $res = $model->orderBy('created_at', 'desc')
            ->paginate($request->get('limit', 30))
            ->toArray();
        // var_dump(DB::getQueryLog());exit;
        return $this->table($res['data'], $res['total']);

    }

    /**
     * @author sql
     * 导入司机列表
     */
    public function importDrivers(Request $request)
    {

        $path = $request->file('file');
        if (empty($path)) {
            return $this->error('请选择上传的文件');
        }
        $delTitle = 1;//指定头行数  删除它
        $excel = new DriverImport($delTitle);
        Excel::import($excel, $path->getRealPath());
        if (!$excel->data) {
            return $this->error('文件内容为空不可以导入');
        }
        $data = $excel->data->toArray();
        //在这可以验证数据
        foreach ($data as $key => &$item) {
            if (empty(trim($item['0'])) == true && empty(trim($item['1'])) == true && empty(trim($item['2'])) == true && empty(trim($item['3'])) == true) {
                unset($data[$key]);
            }

        }
        foreach ($data as $newkey => &$newitem) {
            $hangshu = $newkey + 2;
            if ($newitem) {
                if (empty($newitem[0]) || empty($newitem['1']) || empty($newitem['2'])) {
                    return $this->error('第' . ($hangshu) . '行数据有误，请重新修改');
                    break;
                }

                if (!preg_match('/^1[3456789][0-9]{9}(,1[3456789][0-9]{9})*$/', $newitem['1'])) {
                    return $this->error('第' . ($hangshu) . '行数据手机号格式有误，请重新修改' . $newitem['1']);
                    break;
                }
            }
        }


        $owner_id = $this->owner_id;
        $uuid = $this->uuid;
        // 入库操作
        foreach ($data as &$item) {
            //如果手机号已经存在，就直接修改原数据
            $name = $item['0'];
            $phone = $item['1'];
            $car_no = $item['2'];
            $card_no = $item['3'];
            $where = DriverMailList::query()->where('name', $name)->where('card_no', $card_no)->where('role_id', 10)->where('status', 1)->where('owner_id', $owner_id);
            $info = $where->first();

            if ($info) {
                // $data1['updated_at'] = date('Y-m-d H:i:s', time());
                $data1['owner_id'] = $owner_id;
                $data1['owner_uuid'] = $uuid;
                $data1['name'] = $name;
                $data1['phone'] = $phone;
                $data1['card_no'] = $card_no;
                $data1['car_no'] = $car_no;
                $data1['role_id'] = 10;
                $where->update($data1);

            } else {
                $model = new DriverMailList();
                $model->created_at = date('Y-m-d H:i:s', time());
                $model->owner_id = $owner_id;
                $model->owner_uuid = $uuid;
                $model->name = $name;
                $model->phone = $phone;
                $model->card_no = $card_no;
                $model->car_no = $car_no;
                $model->role_id = 10;
                $model->save();
            }
        }
        return $this->success('导入成功');
    }

    /**
     * @author qiaoli
     * 删除某一个司机的联系方式
     */
    public function changeStatus(Request $request)
    {
        $id = $request->post('id');
        $status = $request->post('status');
        $model = DriverMailList::where('id', $id)->where('owner_id', $this->owner_id);
        $info = $model->get();
        if (!$info) {
            return $this->error('数据不存在');
        }
        $temp['status'] = $status;
        if ($model->update($temp)) {
            return $this->success('删除成功');
        }
        return $this->error('删除失败');


    }
}
